package com.dcbeida.activity.clerkmanager;

import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.os.MessageQueue;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.NestedScrollView;
import android.util.Log;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dcbeida.MyApplication;
import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.adapter.AccountPageAdapter;
import com.dcbeida.utils.Utils;

import java.lang.reflect.Field;

import butterknife.Bind;

/**
 * 管理员账号管理列表
 */
public class AccountListActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRright;
    @Bind(R.id.tl_parent)
    TabLayout tl_parent;
    @Bind(R.id.vp_parent)
    ViewPager vp_parent;

    private AccountPageAdapter mAccountPageAdapter;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_account_list;
    }


    @Override
    public void setData() {
        super.setData();
        MyApplication.getInstance().currentType =0;
        setListener();
        setHeader();
        setChildTab();
        initAdapter();
    }

    private void initAdapter() {
        mAccountPageAdapter = new AccountPageAdapter(this.getSupportFragmentManager());
        vp_parent.setAdapter(mAccountPageAdapter);
    }

    private void setHeader() {
        tvRright.setText("+添加店员");
        tvTitle.setText("店员帐号管理");
        imgBack.setVisibility(View.VISIBLE);
    }


    private void setChildTab() {
        tl_parent.addTab(tl_parent.newTab().setText("已启用"));
        tl_parent.addTab(tl_parent.newTab().setText("未启用"));
        try {
            try {
                Field mTabTextSize = TabLayout.class.getDeclaredField("mTabTextSize");
                mTabTextSize.setAccessible(true);
                mTabTextSize.set(tl_parent, Utils.sp2px(mContext, 16f));
                Field mViewFiled = TabLayout.Tab.class.getDeclaredField("mView");
                mViewFiled.setAccessible(true);
                View mView1 = (View) mViewFiled.get(tl_parent.getTabAt(0));
                Field mTextViewField = mView1.getClass().getDeclaredField("mTextView");
                mTextViewField.setAccessible(true);
                TextView tv_View1 = (TextView) mTextViewField.get(mView1);
                tv_View1.setTypeface(Typeface.DEFAULT_BOLD);

                Field mViewFiled2 = TabLayout.Tab.class.getDeclaredField("mView");
                mViewFiled2.setAccessible(true);
                View mView2 = (View) mViewFiled.get(tl_parent.getTabAt(1));
                Field mTextViewField2 = mView2.getClass().getDeclaredField("mTextView");
                mTextViewField2.setAccessible(true);
                TextView tv_View2 = (TextView) mTextViewField2.get(mView2);
                tv_View2.setTypeface(Typeface.DEFAULT_BOLD);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

    }


    private void setListener() {
        imgBack.setOnClickListener(this);
        tvRright.setOnClickListener(this);
        tl_parent.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                vp_parent.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        vp_parent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                MyApplication.getInstance().currentType = position;
                tl_parent.getTabAt(position).select();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        vp_parent.setCurrentItem(MyApplication.getInstance().currentType);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_right:
                Bundle bundle = new Bundle();
                showActivity(AddAndEditClerkActivity.class, bundle);
                break;
        }
    }
}
